package indi.matcha.csmallproducttest.web;

import indi.matcha.csmallproducttest.ex.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @program: csmall-product
 * @description:
 * @author: Mr.Tang
 * @create: 2023-03-23 20:34
 **/

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true) // 链式
public class JsonResult implements Serializable {
    /**
     * 状态码
     */
    private Integer state;
    /**
     * 操作“失败”时的描述文本
     */
    private String message;

    public static JsonResult OK(){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK);
        return jsonResult;
    }
    public static JsonResult fail(ServiceException e) {
        return fail(e.getState(),e.getMessage());
    }

    public static JsonResult fail(Integer state, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(state);
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
